package com.oocl.jmsbook.producer.file;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.oocl.jmsbook.pojo.Book;
import com.oocl.jmsbook.producer.datagen.BookGenerator;

public class BookJSONFileWriter {

	public static void writeBooksToFile(String filename) {
		ObjectMapper mapper = new ObjectMapper();
		BufferedWriter writer = null;
		try {
			List<Book> bookList = BookGenerator.getRandomBooks(10000);
			writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename)));
			for (Book b : bookList) {
				String bookJson = mapper.writeValueAsString(b);
				writer.write(bookJson + "\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (writer != null) {
				try {
					writer.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
